<?php
require_once 'phpmoko/lib/common.php';
require_once 'phpmoko/externals/JQueryUI.php';
require_once 'phpmoko/ui/RootPanel.php';
require_once 'phpmoko/ui/Form/FormBase.php';

class DateBox extends FormBase
{
	private $value = null;
	private $format = 'mm/dd/yy';
	private $localize = null;
	private $minDate = null;
	private $maxDate = null;
	private $inline = false;

	function __construct()
	{
		parent::__construct();

		JQueryUI::activate();
	}

	function setValue($value)
	{
		$this->value = $value;
	}

	function setToday()
	{
		$this->value = date($this->format);
	}

	function setFormat($value)
	{
		$this->format = $value;
	}

	function setLocalize($value)
	{
		$this->localize = $value;
	}

	function setMinDate($value)
	{
		$this->minDate = $value;
	}

	function setMaxDate($value)
	{
		$this->maxDate = $value;
	}

	protected function setInline($inline)
	{
		$this->inline = $inline;
	}

	function toString()
	{
		$dateid = $this->id;
		$str = '';
		if ($this->inline) {
			$dateid = $this->id . '2';
			$str .= '<div';
			$str .= ' id="' . $dateid . '"';
			$str .= '>';
		}

		$str .= '<input';

		$str .= ' id="' . $this->id . '"';

		if ($this->name != null)
			$str .= ' name="' . $this->name . '"';

		if ($this->style != null)
			$str .= ' class="' . $this->style . '"';

		if ($this->inline)
			$str .= ' type="hidden">';
		else
			$str .= ' type="text">';

		$root = RootPanel::get();

		$js = '$("#' . $dateid . '").datepicker();';

		if ($this->format != null) {
			$js .= '$("#' . $dateid . '").datepicker("option", "dateFormat", \'' . $this->format . '\');';
		}

		if ($this->value != null)
			$js .= '$("#' . $dateid . '").datepicker("setDate", \'' . $this->value . '\');';

		if ($this->minDate != null)
			$js .= '$("#' . $dateid . '").datepicker("option", "minDate", "' . $this->minDate . '");';

		if ($this->maxDate != null)
			$js .= '$("#' . $dateid . '").datepicker("option", "maxDate", "' . $this->maxDate . '");';

		if ($this->localize != null) {
			$root->addJSFile('/pmk-ext/jquery-ui/i18n-min/jquery.ui.datepicker-' . $this->localize . '.min.js');
			$js .= '$("#' . $dateid . '").datepicker("option", $.datepicker.regional["' . $this->localize . '"]);';
		}

		if ($this->inline) {
			$code = '$("#' . $this->id . '").val($("#' . $dateid . '").datepicker().val());';
			$js .= '$("#' . $dateid . '").change( function() {' . $code . '});';
			$js .= $code;
		}

		$root->addJSOnReadyDocument($js);

		return $str;
	}
}
?>